2 if ( ! defined( 'MEDIAWIKI' ) )
5 * PHP <4.2.0 doesn't build ctype_ functions by default and Gentoo doesn't
6 * build it by default, and that probably applies for some other distributions
8 * These functions should be fully compatable with their PHP equivalents
11 * @subpackage Compatability
13 * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
14 * @copyright Copyright © 2006, Ævar Arnfjörð Bjarmason
15 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
17 * @link http://perldoc.perl.org/perlre.html perldoc perlre
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 * Takes the same input and returns the same values as the equvalent PHP
41 function ctype_alnum() {
42 $fname = 'ctype_alnum';
44 $args = func_get_args();
45 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
47 if ( ! is_null( $ret ) )
50 return (bool)preg_match( '~^[[:alnum:]]+$~i', $in );
53 function ctype_alpha() {
54 $fname = 'ctype_alpha';
56 $args = func_get_args();
57 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
59 if ( ! is_null( $ret ) )
62 return (bool)preg_match( '~^[[:alpha:]]+$~i', $in );
65 function ctype_cntrl() {
66 $fname = 'ctype_cntrl';
68 $args = func_get_args();
69 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
71 if ( ! is_null( $ret ) )
74 return (bool)preg_match( '~^[[:cntrl:]]+$~', $in );
77 function ctype_digit() {
78 $fname = 'ctype_digit';
80 $args = func_get_args();
81 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
83 if ( ! is_null( $ret ) )
86 return (bool)preg_match( '~^[[:digit:]]+$~', $in );
89 function ctype_graph() {
90 $fname = 'ctype_graph';
92 $args = func_get_args();
93 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
95 if ( ! is_null( $ret ) )
98 return (bool)preg_match( '~^[[:graph:]]+$~', $in );
101 function ctype_lower() {
102 $fname = 'ctype_lower';
104 $args = func_get_args();
105 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
107 if ( ! is_null( $ret ) )
110 return (bool)preg_match( '~^[[:lower:]]+$~', $in );
113 function ctype_print() {
114 $fname = 'ctype_print';
116 $args = func_get_args();
117 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
119 if ( ! is_null( $ret ) )
122 return (bool)preg_match( '~^[[:print:]]+$~', $in );
125 function ctype_punct() {
126 $fname = 'ctype_punct';
128 $args = func_get_args();
129 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
131 if ( ! is_null( $ret ) )
134 return (bool)preg_match( '~^[[:punct:]]+$~', $in );
137 function ctype_space() {
138 $fname = 'ctype_space';
140 $args = func_get_args();
141 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
143 if ( ! is_null( $ret ) )
146 return (bool)preg_match( '~^[[:space:]]+$~', $in );
151 function ctype_upper() {
152 $fname = 'ctype_upper';
154 $args = func_get_args();
155 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
157 if ( ! is_null( $ret ) )
160 return (bool)preg_match( '~^[[:upper:]]+$~', $in );
164 function ctype_xdigit() {
165 $fname = 'ctype_xdigit';
167 $args = func_get_args();
168 list( $in, $ret ) = _ctype_parse_args( $fname, $args );
170 if ( ! is_null( $ret ) )
173 return (bool)preg_match( '~^[[:xdigit:]]+$~i', $in );
179 function _ctype_parse_args( $fname, $args ) {
182 $cnt = count( $args );
185 // PHP only throws E_ERROR on this, but fuck it;)
186 wfDebugDieBacktrace( "Error: $fname() expects exactly 1 parameter $cnt given" );
188 $in = array_pop( $args );
190 if ( is_int( $in ) ) {
192 // >= 256 returns false, except in the case of these functions
195 $fname === 'ctype_alnum' ||
196 $fname === 'ctype_digit' ||
197 $fname === 'ctype_graph' ||
198 $fname === 'ctype_print' ||
199 $fname === 'ctype_xdigit'
203 else if ( $in >= -128 )
204 $in = ord( $in +
256 );
205 else if ( $in < -128 )
206 // <-128 values return false, except in the case of these functions
210 $fname === 'ctype_graph' ||
$fname === 'ctype_print'
214 if ( is_string( $in ) )
216 return array( null, true );
218 return array( $in, null );
221 return array( null, false );